package in.muton.panels.paginator.helpers;

import in.muton.edushare.pages.common.EdusharePanel;
import in.muton.panels.paginator.badgehelpers.user.BadgeRoles;

import java.util.ArrayList;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.IModel;

import com.edushare.common.vo.Users;

public class RoleChanger extends ListView<String> {
	private static final long serialVersionUID = 30249461402653788L;

	public RoleChanger(String id, BadgeRoles bRole, IModel<Users> userModel,
			EdusharePanel panel) {
		super(id, bRole != null ? bRole.getOtherRoles(userModel.getObject())
				: new ArrayList<String>());
		this.bRole = bRole;
		this.panel = panel;
		this.userModel = userModel;
	}

	@Override
	protected void populateItem(final ListItem<String> arg0) {
		Link<Void> l = null;
		arg0.add(l = new Link<Void>("roleChangeLink") {
			private static final long serialVersionUID = 7255484924645960217L;

			@Override
			public void onClick() {
				panel.clearError();
				panel.setError(bRole.changeRole(userModel.getObject(), arg0
						.getModelObject()));
			}
		});
		l.add(new Label("roleName", arg0.getModelObject()));
	}

	@Override
	public boolean isVisible() {
		if (bRole != null)
			return bRole.isAdmin();
		return false;
	}

	private IModel<Users> userModel;
	private BadgeRoles bRole = null;
	private EdusharePanel panel;
}